﻿namespace Kernel
{
    public class StaticArray
    {
        private ulong base_addr;
        private ulong length;
        private ulong ent_size;

        public StaticArray(ulong Base, ulong ByteLength)
        { base_addr = Base; length = ByteLength; ent_size = 1; }
        public StaticArray(ulong Base, ulong ByteLength, ulong ElemSize)
        { base_addr = Base; length = ByteLength; ent_size = ElemSize; }

        public void Clear(ushort v)
        {
            int j = 0;
            for (ulong i = 0; i < length; i++)
            {
                ABI.MemoryOperations.Poke(base_addr + i, (byte)(v >> (j * 8)));
                j++;
                if (j == 2)
                    j = 0;
            }
        }
        public void Clear(byte v)
        { for (ulong i = 0; i < length; i++) ABI.MemoryOperations.Poke(base_addr + i, v); }
        public void Clear()
        { Clear(0); }

        public void Set(ulong addr, byte v)
        {
            if ((addr * ent_size + 1) > length)
                throw new System.IndexOutOfRangeException();
            ABI.MemoryOperations.Poke(base_addr + addr * ent_size, v);
        }
        public void Set(ulong addr, char v)
        {
            if ((addr * ent_size + 2) > length)
                throw new System.IndexOutOfRangeException();
            ABI.MemoryOperations.Poke(base_addr + addr * ent_size, v);
        }

        public byte this[ulong i]
        {
            get
            {
                if (length <= i)
                    throw new System.IndexOutOfRangeException();
                return ABI.MemoryOperations.PeekU1(base_addr + i * ent_size);
            }

            set
            {
                if (i >= length)
                    throw new System.IndexOutOfRangeException();
                ABI.MemoryOperations.Poke(base_addr + i * ent_size, value);
            }
        }

    }
}
